<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?php 
	unset($_SESSION['SERVICES_INFO']); 
	
	$USER = getAccountUserObject();
	$ESCORT = $this->escort_entity->init('user_id',$USER->getId());
	
	$subscriptionArray = array(); 
	$subsJsonArray = array();
	foreach(getSubcriptionData_ioc() as $key=>$arrayValue){
		$subscriptionArray[$key] =  $arrayValue[0].' '.$arrayValue[1].' &euro;';
		$subsJsonArray[$key] = $arrayValue[1];
	}
	 
	$listPlaceArray = array();
	$placeJsonArray = array();
	foreach(getPlaceListWorkData_ioc() as $key=>$arrayValue){
		$listPlaceArray[$key] = $arrayValue[0].' '.$arrayValue[1].' &euro;';
		$placeJsonArray[$key] = $arrayValue[1];
	}
	
	$listPackArray = array();
	$packJsonArray = array();
	foreach(getPackListData_ioc() as $key=>$arrayValue){
		$listPackArray[$key] = $arrayValue[0].' '.$arrayValue[1].' &euro;';
		$packJsonArray[$key] = $arrayValue[1];
	}
	
	$listPriorityArray = array();
	$priorityJsonArray = array();
	foreach(getPriorityData_ioc() as $key=>$arrayValue){
		$listPriorityArray[$key] = $arrayValue[0].' '.$arrayValue[1].' &euro;';
		$priorityJsonArray[$key] = $arrayValue[1];
	}
	
	$totalPeriodDiscount = $totalPromoDiscount = 0;
	$iC = 0;
?>
<style>
	
</style>

<script type="text/javascript">
	;var DISCOUNT = JSON.parse('<?php echo json_encode(contractDiscount_ioc());?>');
	var subs = JSON.parse('<?php echo json_encode($subsJsonArray);?>');
	var place = JSON.parse('<?php echo json_encode($placeJsonArray);?>');
	var pack = JSON.parse('<?php echo json_encode($packJsonArray);?>');
	var priority = JSON.parse('<?php echo json_encode($priorityJsonArray);?>');
	var EURO_MONTHLY = ' <?php echo '&euro;';//language_translate('services_page_euro_monthly');?>';
	var PROMO_AMOUNT = 0;
	var PROMO_UNIT = '';
	
	function returnValueJSON(json,key){
		return parseFloat( json[key] );
	}
</script>

<form action="<?php echo site_url('escorts/transaction_submit/submit_services');?>" name="submit_services" id="submit_services" method="post" enctype="multipart/form-data" >

	<div id="content-left">
		<div id="personal-tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
			<div id="tabs-1" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
				<!-- content -->	
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-right">
							<?php echo language_translate('services_page_your_current_service');?>
						</div>
						<div class="clear"></div>
					</div>
					
					<?php 
						$PAYMENT = $this->payment_log_entity->getNetPriceAmount($ESCORT->getId());
						$subscription_id = $this->payment_log_entity->getServicesTypePaid($ESCORT->getId(), 'SUBS');
						$myPlaceListArray = array_filter( explode(',', $this->payment_log_entity->getServicesTypePaid($ESCORT->getId(), 'PLACE')) );
						$myPackListArray = array_filter( explode(',', $this->payment_log_entity->getServicesTypePaid($ESCORT->getId(), 'PACK')) );
						$myPriorityListArray = array_filter( explode(',', $this->payment_log_entity->getServicesTypePaid($ESCORT->getId(), 'PRI')) );
					?>
					
					<div class="dashboard-info-wrapper">
						<div class="select-box">
								<table width="100%">
									<thead>
										<th><?php echo language_translate('services_page_table_head_servicename');?></th>
										<th><?php echo language_translate('services_page_table_head_details');?></th>
										<th><?php echo language_translate('contract_start_date');?></th>
										<th><?php echo language_translate('contract_to_date');?></th>
										<th><?php echo language_translate('services_page_table_head_period');?></th>
										<th style="text-align:right;"><?php echo language_translate('services_page_table_head_priceeuro');?></th>
									</thead>
									<tbody>
										<?php if($subscription_id):?>
											<tr>
												<?php 
													$SUB = $this->payment_log_entity->checkServiceExisted($ESCORT->getId(), 'SUBS', $subscription_id);
													$sub_period = $SUB ? $SUB->getLatestPeriod() : 1;
													$subs_array = getSubcriptionData_ioc($subscription_id);
													$totalPeriodDiscount += $SUB->discount_period_price;
													$totalPromoDiscount += $SUB->discount_promo_price;
													$iC++;
												?>
												<td><?php echo language_translate('services_page_subscription');?></td>
												<td><?php echo $subs_array[0];?></td>
												<td><?php echo sysDateFormat( $SUB->latest_payment_date ,DATE_FORMAT );?></td>
												<td><?php echo sysDateFormat( $SUB->apply_date ,DATE_FORMAT );?></td>
												<td><?php echo $sub_period;?></td>
												<td style="text-align:right;"><?php echo euroCurrencyFormat(format ( $SUB->getPaymentPrice() ) );?></td>
											</tr>
										<?php endif;?>
										<?php
											foreach($myPlaceListArray as $k):
												$placeInfo = getPlaceListDataInfoById($k);
										?>
											<?php if($placeInfo):?>
												<?php 
													$id=$placeInfo['id'];
													$PLACE = $this->payment_log_entity->checkServiceExisted($ESCORT->getId(), 'PLACE', $id);
													$place_period = $PLACE ? $PLACE->getLatestPeriod() : 1;
													$totalPeriodDiscount += $PLACE->discount_period_price;
													$totalPromoDiscount += $PLACE->discount_promo_price;
													$iC++;
												?>
												<tr>
													<td><?php echo language_translate('services_page_where_do_you_work');?></td>
													<td>
														<?php echo $placeInfo['country'];?>
														<?php if($placeInfo['state']):?> 
															/
															<?php echo $placeInfo['state'];?>
														<?php endif;?>	
														<?php if($placeInfo['province']):?> 
															/
															<?php echo $placeInfo['province'];?>
														<?php endif;?>
														<?php if($placeInfo['city']):?> 
															/
															<?php echo $placeInfo['city'];?>
														<?php endif;?>
													</td>
													<td><?php echo sysDateFormat( $PLACE->latest_payment_date ,DATE_FORMAT );?></td>
													<td><?php echo sysDateFormat( $PLACE->apply_date ,DATE_FORMAT );?></td>
													<td>
														<?php echo $place_period;?>
													</td>
													<td style="text-align:right;"><?php echo euroCurrencyFormat(format ( $PLACE->getPaymentPrice() ) );?></td>
												</tr>
											<?php endif;?>
										<?php endforeach; ?>
										
										<?php foreach($myPackListArray as $k):?>
												<?php 
													$id=$k;
													$PACK = $this->payment_log_entity->checkServiceExisted($ESCORT->getId(), 'PACK', $id);
													$pack_period = $PACK ? $PACK->getLatestPeriod() : 1;
													$pack_array = getPackListData_ioc($id);
													$totalPeriodDiscount += $PACK->discount_period_price;
													$totalPromoDiscount += $PACK->discount_promo_price;
													$iC++;
												?>
												<tr>
													<td><?php echo language_translate('services_page_add_a_pack');?></td>
													<td>
														<?php echo $pack_array[0];?>
													</td>
													<td><?php echo sysDateFormat( $PACK->latest_payment_date ,DATE_FORMAT );?></td>
													<td><?php echo sysDateFormat( $PACK->apply_date ,DATE_FORMAT );?></td>
													<td>
														<?php echo $pack_period;?>
													</td>
													<td style="text-align:right;"><?php echo euroCurrencyFormat(format ( $PACK->getPaymentPrice() ) );?></td>
												</tr>
										<?php endforeach; ?>
										
										<?php foreach($myPriorityListArray as $k):?>
												<?php 
													$id=$k;
													$PRI = $this->payment_log_entity->checkServiceExisted($ESCORT->getId(), 'PRI', $id);
													$pri_period = $PRI ? $PRI->getLatestPeriod() : 1;
													$pri_array = getPriorityData_ioc($id);
													$totalPeriodDiscount += $PRI->discount_period_price;
													$totalPromoDiscount += $PRI->discount_promo_price;
													$iC++;
												?>
												<tr>
													<td><?php echo language_translate('services_page_priority');?></td>
													<td>
														<?php echo $pri_array[0];?>
													</td>
													<td><?php echo sysDateFormat( $PRI->latest_payment_date ,DATE_FORMAT );?></td>
													<td><?php echo sysDateFormat( $PRI->apply_date ,DATE_FORMAT );?></td>
													<td>
														<?php echo $pri_period;?>
													</td>
													<td style="text-align:right;"><?php echo euroCurrencyFormat(format ( $PRI->getPaymentPrice() ) );?></td>
												</tr>
										<?php endforeach; ?>
										<tr>
											<td colspan=5>
												<?php echo language_translate('services_page_net_amount');?>
											</td>
											<td style="text-align:right;"><?php if($PAYMENT AND $iC) echo euroCurrencyFormat($PAYMENT);?></td>
										</tr>
									</tbody>
								</table>
						 </div>
						<div class="clear"></div>
					</div>
					
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-left"><span><?php echo language_translate('services_page_promo_discount');?></span></div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<?php echo euroCurrencyFormat( format($totalPromoDiscount) );?> &euro;
							</div>
							<div class="clear"></div>
						</div>
						<div class="clear"></div>
					</div>
					
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-left"><span><?php echo language_translate('services_page_total_period_discount');?></span></div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<?php echo euroCurrencyFormat( format($totalPeriodDiscount));?> &euro;
							</div>
							<div class="clear"></div>
						</div>
						<div class="clear"></div>
						<hr class="background-separate"/>
					</div>
				
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-right">
							<?php echo language_translate('services_page_edit_your_service');?>
						</div>
						<div class="clear"></div>
					</div>
				<?php 
					$subscription_id = $this->payment_log_entity->getServiceTypeStringIdsCurrently($ESCORT->getUserId(), 'SUBS');
					$myPlaceListArray = explode(',', $this->payment_log_entity->getServiceTypeStringIdsCurrently($ESCORT->getUserId(), 'PLACE') );
					$myPackListArray = explode(',', $this->payment_log_entity->getServiceTypeStringIdsCurrently($ESCORT->getUserId(), 'PACK') );
					$myPriorityListArray = explode(',', $this->payment_log_entity->getServiceTypeStringIdsCurrently($ESCORT->getUserId(), 'PRI') );
					$period = $this->payment_log_entity->getServicePeriodCurrently($ESCORT->getUserId());
				?>
					
					<div class="clear"></div>
					
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_subscription');?></span>
							<div class="help-text" title="<?php echo language_translate('services_page_subscription_hint');?>"></div>
						</div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<?php echo form_dropdown('subscription', $subscriptionArray, array($subscription_id) , 'id="subscription" class="normal"' );?>
							</div>
							<div class="select-box">
								<?php 
									//$SUB = $this->payment_log_entity->checkServiceExisted($ESCORT->getId(), 'SUBS', $subscription_id);
									//$sub_period = $SUB ? $SUB->getLatestPeriod() : 1;
								?>
								<?php echo contractDiscountSelectionForm('subscription_period','subs','onchange="return cal_discount_subs(this.value);" name="subscription_period"', $period );?>
							</div>
						 </div>
						<div class="clear"></div>
					</div>	
			
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_where_do_you_work');?></span>
							<div class="help-text" title="<?php echo language_translate('services_page_where_do_you_work_hint');?>"></div>
						</div>
						<div class="dashboard-info-right">
							<div class="select-box" style="margin:0px;">
								
								<?php 
									//echo form_dropdown('place_listing', $listPlaceArray, array() , 'id="place_listing" class="normal" onchange="return loadListingPlaceElement(this.value);"' );
									$this->load->model('home/escort_zone_m');
									//echo $this->home_m->listingPlaceLocation();
								?>
								<div id="service_country">
									<?php echo $this->escort_zone_m->services_load_country();?>
								</div>
								
							<!--	<div class="clear" style="height:5px;"></div>	-->
								
								<div id="service_state">
									<?php echo $this->escort_zone_m->services_load_state(0);?>
								</div>
								
							<!--	<div class="clear" style="height:5px;"></div>	-->
							
								<div id="service_province">
									<?php echo $this->escort_zone_m->services_load_province(0,0);?>
								</div>
							<!--	
								<div class="clear" style="height:5px;"></div>
								<div id="service_city">
									<?php //echo $this->escort_zone_m->services_load_city(0,0,0);?>
								</div>
							-->	
								<div class="clear" style="height:5px;"></div>
									<?php echo $this->escort_zone_m->add_button_js();?>
								<div class="clear"></div>	
								<?php echo loader_image_s("id='listingPlaceLoadingImage' class='hidden'");?>
							</div>
							<div class="clear"></div>
							<div class="select-box" style="margin-top:5px; ">
								<table width="100%">
									<thead>
										<th><?php echo language_translate('services_page_table_head_country');?> 
										 / <?php echo language_translate('services_page_table_head_state');?> 
										 / <?php echo language_translate('services_page_table_head_province');?> 
										 / <?php echo language_translate('services_page_table_head_city');?></th>
										<th><?php echo language_translate('services_page_table_head_price');?></th>
										<th><?php echo language_translate('services_page_table_head_action');?></th>
										 
									</thead>
									<tbody id="listingPlaceTarget">
										<?php
											foreach($myPlaceListArray as $k):
											$placeInfo = getPlaceListDataInfoById($k);
										?>
											<?php if($placeInfo):?>
											<?php $id=$placeInfo['id'];?>
											<tr id="listing_id_<?php echo $id;?>">
												<td>
													<?php echo $placeInfo['country'];?>
													<?php if($placeInfo['state']):?> 
														/
														<?php echo $placeInfo['state'];?>
													<?php endif;?>	
													<?php if($placeInfo['province']):?> 
														/
														<?php echo $placeInfo['province'];?>
													<?php endif;?>
													<?php if($placeInfo['city']):?> 
														/
														<?php echo $placeInfo['city'];?>
													<?php endif;?>
												</td>
												<td>
													<?php echo $placeInfo['price'].'&euro;';?>
												</td>
												<td>
													<?php echo delete_image("class='deleteItem' onclick='return deleteListingItem($id);'");?>
												</td>
												<input type="hidden" name="list_place[]" value="<?php echo $id;?>" />
											</tr>
											<?php endif;?>
										<?php endforeach; ?>
									</tbody>
								</table>
							</div>
							<div class="clear"></div>
							<div id="placeLISTwarning"><?php echo language_translate('contract_listing_place_warning');?></div>
						 </div>
						<div class="clear"></div>
					</div>
					
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_add_a_pack');?></span>
							<div class="help-text" title="<?php echo language_translate('services_page_add_a_pack_hint');?>"></div>
						</div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<table width="100%">
									<thead>
										<th><?php echo language_translate('services_page_table_head_packname');?> 
										<th><?php echo language_translate('services_page_table_head_price');?></th>
										<th><?php echo language_translate('services_page_table_head_action');?></th>
									</thead>
									<tbody>
										<?php
											foreach(getPackListData_ioc() as $k =>$v):
										?>
											<tr>
												<td>
													<?php echo $v[0];?>
												</td>
												<td>
													<?php echo $v[1].' &euro;';?>
												</td>
												<td>
													<?php 
														if(in_array($k, $myPackListArray)){
															$checked = 'checked="checked"';
														}else{
															$checked = '';
														}
													?>
													<input type="checkbox" name="list_pack[]" value="<?php echo $k;?>" <?php echo $checked;?> class="custom" />
												</td>
												
											</tr>
										<?php endforeach; ?>
									</tbody>
								</table>
							</div>
						 </div>
						<div class="clear"></div>
					</div>
					
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_priority');?></span>
							<div class="help-text" title="<?php echo language_translate('services_page_priority_hint');?>"></div>
						</div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<table width="100%">
									<thead>
										<th><?php echo language_translate('services_page_table_head_priority');?> 
										<th><?php echo language_translate('services_page_table_head_price');?></th>
										<th><?php echo language_translate('services_page_table_head_action');?></th>
									</thead>
									<tbody>
										<?php
											foreach(getPriorityData_ioc() as $k =>$v):
										?>
											<tr>
												<td>
													<?php echo $v[0].' (+'.$v[2].')';?>
												</td>
												<td>
													<?php echo $v[1].' &euro;';?>
												</td>
												<td>
													<?php 
														if(in_array($k, $myPriorityListArray)){
															$checked = 'checked="checked"';
														}else{
															$checked = '';
														}
													?>
													<input type="radio" name="list_priority" value="<?php echo $k;?>" <?php echo $checked;?> class="custom" /> 
													<!--<input type="checkbox" name="list_priority[]" value="<?php echo $k;?>" <?php echo $checked;?> class="custom onlyone" /> -->
												</td>
											</tr>
										<?php endforeach; ?>
									</tbody>
								</table>	
								<?php
									//echo multi_checkbox('list_priority[]',$listPriorityArray,explode(',', $ESCORT->getPriorityIds()));				
								?>
							</div>
							 
						 </div>
						<div class="clear"></div>
					</div>
					
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_your_promotion_code');?></span>
							<div class="help-text" title="<?php echo language_translate('services_page_your_promotion_code_hint');?>"></div>
						</div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<span id="promotion-info">
									
								</span>
							</div>
							<div class="clear"></div>
							<div class="select-box" id="promotioncode-input" style="margin-top:10px;">
								<div class="select-box">
									<input type="text" name="promotion_code" id="promotion_code" value="" />
								</div>
								<div class="select-box">
									<input type="button" onclick="return do_save_change_promotion_code();" class="save-button" value="<?php echo language_translate('appareance_save');?>" /> 
								</div>
								<span style="float:left;"><?php echo loader_image_s('id="id_loader_pic" class="hidden"');?></span>
							</div>
						</div>
						<div class="clear"></div>
					</div>
					
					<div class="clear"></div>
					
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_total_price');?></span>
						</div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<span id="total_price" style="float:left;"></span> 
							</div>
						</div>
						
						<div class="clear" style="margin:3px 0px;"></div>
						
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_total_period_discount');?></span>
						</div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<span id="period_discount_price" style="float:left;"></span> 
							</div>
						</div>
						
						<div class="clear" style="margin:3px 0px;"></div>
						
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_promo_discount');?></span>
						</div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<span id="promo_discount_price" style="float:left;"></span> 
							</div>
						</div>
						
						<div class="clear" style="margin:3px 0px;"></div>
						
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_current');?></span>
						</div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<span id="current_price" style="float:left;"></span> 
							</div>
						</div>
						
						<div class="clear" style="margin:3px 0px;"></div>
						
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_net_amount');?></span>
						</div>
						<div class="dashboard-info-right">
							<span style="float:left;" id="net_amount_price"></span>
							<span style="float:left;"><?php echo loader_image_s('id="id_loader_pic_1" class="hidden"');?></span>
						</div>
						<div class="clear"></div>
					</div>
					
					<div class="clear"></div>
					
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-left">
							<span><?php echo language_translate('services_page_payment_gateway');?></span>
							<div class="help-text" title="<?php echo language_translate('services_page_payment_gateway_hint');?>"></div>
						</div>
						<div class="dashboard-info-right">
							<div class="select-box">
								<?php echo form_dropdown('payment_gateway', paymentGatewayType(), array() , 'id="payment_gateway" class="normal"' );?>
							</div>
						 </div>
						<div class="clear"></div>
					</div>	
					
					<div class="dashboard-info-wrapper">
						<div class="dashboard-info-left"><span>&nbsp;</span></div>
						<div class="dashboard-info-right">
							<input type="submit" class="save-button" value="<?php echo language_translate('services_page_contract');?>" /> 
							<?php echo loader_image("id='loader_image' class='hidden'");?>
							<input type="button" onclick="return gotoPaymentPage();" id="payment-button" class="save-button hidden" 
								value="<?php echo language_translate('services_page_pay');?>" /> 
						</div>
						<div class="clear"></div>
					</div>	 
					
					<div class="dashboard-info-wrapper">
						<div id="update-message"><?php echo language_translate('update_successfully_message');?></div>
						<div class="clear"></div>
					</div>
	
				<!-- end content -->
			</div>
		</div>
	</div>

</form>

<?php $this->load->view('escorts/right_nav'); ?>



<script type="text/javascript">
	$(document).ready(function() { 
		$('.help-text[title]').qtip({ style: { name: 'dark', tip: true } });
	});	
	
	var CONTRACT_ENABLE = 0;
	
	function calNetAmount(){
		$('#id_loader_pic_1').show();
		
		var period = $('#subscription_period').val();
		var subscription_id = $('#subscription').val();
		var list_place = getArrayNameTextInput('list_place').join(',');
		var list_pack = getMultiCheckbox('list_pack').join(',');
		var list_priority = $('input[name="list_priority"]:checked').val();//getMultiCheckbox('list_priority').join(',');
		var promocode = $('#promotion_code').val();
		list_priority = (list_priority) ? list_priority:'';
		CONTRACT_ENABLE = 0;
		$.post(
			BASE_URI+'escorts/transaction_submit/calNetAmount',
			{	
				period:period,
				subscription_id:subscription_id,list_place:list_place,
				list_pack:list_pack,list_priority:list_priority,
				promocode:promocode
			},
			function(res){
				$('#net_amount_price').html(res.netamount + EURO_MONTHLY);
				$('#total_price').html(res.totalprice + EURO_MONTHLY);
				$('#period_discount_price').html(res.perioddiscount + EURO_MONTHLY);
				$('#current_price').html(res.currentprice + EURO_MONTHLY);
				$('#promo_discount_price').html(res.promodiscount + EURO_MONTHLY);
				
				$('#id_loader_pic_1').hide();
				CONTRACT_ENABLE = 1;
			}
		,'json');
	}

	$(document).ready(function() {
		var options = { 
			beforeSubmit:  validateB4Submit,  
			success:       processAfterResponding   
		};	
		$('#submit_services').ajaxForm(options); 
		
		$('#subscription').live('change',function(){	 
			updatePriceJS();
		});
		$('.jquery-safari-checkbox').bind('click',function(){updatePriceJS();});
		$('input[name="list_priority"]').bind('click',function(){updatePriceJS();});
		do_save_change_promotion_code();
	});	
	
	function updatePriceJS(){
		/*
		var net_price = parseFloat( calPriceTotalUnDiscount() - calPeriodTotalDiscount() - calPromoTotalDiscount() ); 
		net_price = net_price.toFixed(2);
		net_price = (net_price <= 0) ? 0:net_price;
		
		$('#total_price').html( calPriceTotalUnDiscount() + EURO_MONTHLY );
		$('#period_discount_price').html( calPeriodTotalDiscount() + EURO_MONTHLY );
		$('#promo_discount_price').html(calPromoTotalDiscount() + EURO_MONTHLY);
		$('#current_price').html( net_price + EURO_MONTHLY );
		*/
		calNetAmount();
	}
	
	function calPromoTotalDiscount(){
		if(PROMO_AMOUNT == 0){
			return 0;
		}
		if(PROMO_UNIT == ''){
			return 0;
		}
		$payment_price = calPriceTotalPeriodDiscount();
		$sub_period = $('#subscription_period').val();
		
		if(PROMO_UNIT == 'MONTH'){
			$tmp = ( $payment_price * PROMO_AMOUNT)/$sub_period;
		}
		if(PROMO_UNIT == 'PERCENT'){
			$tmp = ($payment_price * PROMO_AMOUNT) /100 ;
		}
		if(PROMO_UNIT == 'EURO'){
			$tmp = PROMO_AMOUNT;
		}
		return $tmp.toFixed(2);
	}
	
	function calPriceTotalPeriodDiscount(){
		var price = 0;
		price += cal_Subs();
		price += cal_Listing();
		price += cal_Pack();
		price += cal_Priority();
		price = price.toFixed(2);
		return price;
	}
	
	function calPeriodTotalDiscount(){
		$sub_period = $('#subscription_period').val();
		$subscription_id = $('#subscription').val();
		var price = returnValueJSON(subs,$subscription_id);
		var discount = returnValueJSON(DISCOUNT,$sub_period);
		var endPrice = (price*discount)/100;
		
		var totalPrice = calPriceTotalUnDiscount();
		endPrice = totalPrice*discount/100; 
		return endPrice.toFixed(2);
		/*
		$list_place = getArrayNameTextInput('list_place');
		for(var i=0; i<$list_place.length; i++){
			var orgPrice = returnValueJSON(place,$list_place[i]);
			discount = returnValueJSON(DISCOUNT,$sub_period);
		    endPrice += (orgPrice*discount)/100;
		}
		
		$list_pack = getMultiCheckbox('list_pack');
		for(var i=0; i<$list_pack.length; i++){
			var orgPrice = returnValueJSON(pack,$list_pack[i]);
			discount = returnValueJSON(DISCOUNT,$sub_period);
		    endPrice += (orgPrice*discount)/100;
		}
		
		$list_priority = getMultiCheckbox('list_priority');
		for(var i=0; i<$list_priority.length; i++){
			var orgPrice = returnValueJSON(priority,$list_priority[i]);
			discount = returnValueJSON(DISCOUNT,$sub_period);
			endPrice += (orgPrice*discount)/100;
		}
		return endPrice.toFixed(2);
		*/
	}
	
	function calPriceTotalUnDiscount(){
		$subscription_id = $('#subscription').val();
		$list_place = getArrayNameTextInput('list_place');
		$list_pack = getMultiCheckbox('list_pack');
		$list_priority = getMultiCheckbox('list_priority');
		
		var price = 0;
		price += returnValueJSON(subs,$subscription_id);
		
		for(var i=0; i<$list_place.length; i++){
			price += returnValueJSON(place,$list_place[i]);
		}
		for(var i=0; i<$list_pack.length; i++){
			price += returnValueJSON(pack,$list_pack[i]);
		}
		for(var i=0; i<$list_priority.length; i++){
			price += returnValueJSON(priority,$list_priority[i]);
		}
		$sub_period = $('#subscription_period').val();
		return ($sub_period*price).toFixed(2);
	}
	
	function cal_Subs(){
		$subscription_id = $('#subscription').val();
		$sub_period = $('#subscription_period').val();
		
		var price = returnValueJSON(subs,$subscription_id);
		var discount = returnValueJSON(DISCOUNT,$sub_period);
		var endPrice = price*$sub_period - (price*discount)/100;
		return endPrice;
	}
	
	function cal_Listing(){
		$list_place = getArrayNameTextInput('list_place');
		var price = 0;
		for(var i=0; i<$list_place.length; i++){
			var orgPrice = returnValueJSON(place,$list_place[i]);
			$sub_period = $('#subscription_period').val();
			
			var discount = returnValueJSON(DISCOUNT,$sub_period);
			var endPrice = orgPrice*$sub_period - (orgPrice*discount)/100;
			price += endPrice;
		}
		return price;
	}
	
	function cal_Pack(){
		$list_pack = getMultiCheckbox('list_pack');
		var price = 0;
		for(var i=0; i<$list_pack.length; i++){
			var orgPrice = returnValueJSON(pack,$list_pack[i]);
			$sub_period = $('#subscription_period').val();
			
			var discount = returnValueJSON(DISCOUNT,$sub_period);
			var endPrice = orgPrice*$sub_period - (orgPrice*discount)/100;
			price += endPrice;
		}
		return price;
	}
	
	function cal_Priority(){
		$list_priority = getMultiCheckbox('list_priority');
		var price = 0;
		for(var i=0; i<$list_priority.length; i++){
			var orgPrice = returnValueJSON(priority,$list_priority[i]);
			$sub_period = $('#subscription_period').val();
			
			var discount = returnValueJSON(DISCOUNT,$sub_period);
			var endPrice = orgPrice*$sub_period - (orgPrice*discount)/100;
			price += endPrice;
		}
		return price;
	}
	
	function cal_discount_priority(id,val){
		updatePriceJS();
	}
	
	function cal_discount_pack(id, val){
		updatePriceJS();
	}
	
	function cal_discount_listplace(id,val){
		updatePriceJS();
	}
	
	function cal_discount_subs(val){
		updatePriceJS();
	}
	
	function validateB4Submit(formData, jqForm, options){
		if(CONTRACT_ENABLE == 0){
			return false;
		}
        /*
		if($('#payment_gateway').val() != 'wirebank'){
			alert('<?php echo language_translate('services_page_payment_gateway_not_support');?>');
			return false;
		}
        */
		$('#loader_image').show();
		CONTRACT_ENABLE = 0;
		return true;
	}

	function processAfterResponding(responseText, statusText, xhr, $form) {
		$('#loader_image').hide();	
		if($('#payment_gateway').val() == 'wirebank'){
			gotoPaymentPage();
		}else{
		  gotoCreditcardPaymentPage();
		}
		/*
		if(responseText == 'ok'){
			if($('#payment_gateway').val() == 'wirebank'){
				gotoPaymentPage();
			}
		}else{
			debug(responseText);
		}
		*/
	}
    
    function gotoCreditcardPaymentPage(){
        queryurl(BASE_URI+"escorts/transaction_submit/creditcard_method");
    }
	
	function gotoPaymentPage(){
		queryurl(BASE_URI+"escorts/transaction_submit/payment");
	}
	
	function openInputPromitionCode(){
		$('#promotioncode-input').toggle();	
	}
	
	function do_save_change_promotion_code(){
		$('#id_loader_pic').toggle();
		$.post(BASE_URI+'escorts/async/save_promotion_code',{code:$('#promotion_code').val()},function(res){
			$('#promotion-info').html(res.html);
			$('#id_loader_pic').toggle();
		    PROMO_AMOUNT = parseInt( res.amount );
			PROMO_UNIT = res.unit;
			updatePriceJS();
		}, 'json');
	}

	function deleteListingItem(id){
		$('#listing_id_'+id).remove();
		updatePriceJS();
	}
	
	function loadListingPlaceElement(){
		$('#listingPlaceLoadingImage').toggle();

		country_id = $('#service_country_id').val();
		state_id = $('#service_state_id').val();
		province_id = $('#service_province_id').val();
		city_id = $('#service_city_id').val();
  
		$listingIdArray = getArrayNameTextInput('list_place');
		
		$.get(BASE_URI+'home/services_reg_async/fetch_listing_id',
				{country_id:country_id,state_id:state_id,province_id:province_id,city_id:city_id},
			function(listingId){
				if($listingIdArray.in_array(listingId)){
					$('#listingPlaceLoadingImage').toggle();
					return false;
				}else{
					$.get(BASE_URI+'escorts/async/load_listing_place_element',{id:listingId},function(res){
						$('#listingPlaceLoadingImage').toggle();
						$('#listingPlaceTarget').append(res);
						$('#listing_id_'+listingId+' select.custom').customStyle();
						updatePriceJS();
					});
				}
			}	
		);		
	}
</script>